package com.lohit.dbtest;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.sax.StartElementListener;

import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

@SuppressLint("ValidFragment")
public class CustomAdapter extends BaseAdapter {

	
	private ArrayList<AppointmentDetails> data;
	
	final String s1 = "09:30 AM";

	final String s2 = "10:00 AM";
	
	final String s3 = "10:30 AM";
	
	final String s4 = "11:00 AM";
	
	final String s5 = "11:30 AM";
	

	final String s6 = "12:00 PM";

	final String s7 = "12:30 PM";

	
	final String s8 = "01:00 PM";
	
	final String s9 = "01:30 PM";

	final String s10 = "03:00 PM";

	final String s11 = "03:30 PM";

	final String s12 = "04:00 PM";

	final String s13 = "04:30 PM";

	final String s14 = "05:00 PM";

	final String s15 = "05:30 PM";

	final String s16 = "06:00 PM";

	final String s17 = "06:30 PM";

	final String s18 = "07:00 PM";

	final String s19 = "07:30 PM";
	
	final String s20 = "08:00 PM";


	static final int TIME_DIALOG_ID = 0;
	Context context;
	DbController controller;
	int hour, minute;

	public CustomAdapter(ArrayList<AppointmentDetails> data, Context context) {

		this.data = data;

		this.context = context;

		controller = new DbController(context);

	}

	@Override
	public int getCount() {

		return data.size();
	}

	@Override
	public Object getItem(int pos) {

		return (data.get(pos) == null) ? null : data.get(pos);
	}

	@Override
	public long getItemId(int arg0) {

		return arg0;
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		View v = convertView;

		if (v == null) {

			LayoutInflater vi = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			v = vi.inflate(R.layout.row, null);

		}

		TextView name = (TextView) v.findViewById(R.id.patientNameTextView);
		final TextView date = (TextView) v.findViewById(R.id.dateTextView);

		Button reSchedule = (Button) v.findViewById(R.id.chooseDateButton);

		final CheckBox status = (CheckBox) v.findViewById(R.id.statusCheckBox);
	ImageButton cancelAppointment = (ImageButton) v
				.findViewById(R.id.cancelAppointment);

		AppointmentDetails detail = data.get(position);

		long aid = detail.getAid();

		Date temp = detail.getAppDate();

		final String illness = detail.getIllness();

		Log.d("getIllness", detail.getIllness());

		final String number = detail.getPatientNumber();

		SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
		SimpleDateFormat timeFormatAP = new SimpleDateFormat("hh:mm aa");

		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

		final String displayTime = timeFormat.format(temp);
		String dt = timeFormatAP.format(temp);
		Log.d("hourss", displayTime);

		final String displayDate = dateFormat.format(temp);

		final String patientName = detail.getPatientName();

		final String location = detail.getaLocation();

		final String s_aid = String.valueOf(aid);

		name.setText(detail.getPatientName());

		date.setText(dt);

		status.setChecked(false);

		reSchedule.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent i = new Intent(context, RescheduleActivity.class);

				Log.d("before intent", illness);

				i.putExtra("sickness", illness);
				i.putExtra("aid", s_aid);

				i.putExtra("patientName", patientName);
				i.putExtra("number", number);
				i.putExtra("date", displayDate);
				i.putExtra("time", displayTime);
				i.putExtra("location", location);

				context.startActivity(i);

			}
		});

		status.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {

				if (isChecked) {
					AlertDialog.Builder builder = new AlertDialog.Builder(
							context);
					builder.setTitle("Do you want to set this appointment as complete?");

					builder.setPositiveButton("yes",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {

									mess();

									controller.setStatus(s_aid);
									
								
									
									data.remove(position);
									
									notifyDataSetChanged();

								}
							});
					builder.setNegativeButton("no",
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int id) {

									status.setChecked(false);
									dialog.cancel();

								}

							});
					builder.create().show();

				}

			}
		});

		cancelAppointment.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
				
				
				// TODO Auto-generated method stub
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				
				
				builder.setTitle("Do you want cancel this appointment?");
				
				builder.setPositiveButton("Yes",

new DialogInterface.OnClickListener() {
							
					public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								controller.cancelAppointment(s_aid);
								
								Toast.makeText(context, displayTime, Toast.LENGTH_SHORT).show();
								
								Toast.makeText(context, date.getText().toString(), Toast.LENGTH_SHORT).show();
								
								Toast.makeText(context, displayDate, Toast.LENGTH_SHORT).show();
							
								
								uncheckSlot( date.getText().toString(), displayDate);
								
								
								data.remove(position);
								
								notifyDataSetChanged();
							}
						});

				builder.setNegativeButton("No",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
							}
						});
				builder.create().show();
			};
		});
		return v;
	}

	@SuppressLint("ValidFragment")
	public void mess() {

		Toast.makeText(context, "Status Updated", Toast.LENGTH_SHORT).show();

	}
	
	
	
	public void uncheckSlot(String oldTime , String aDate) {

		if (oldTime.equals(s1)) {
			Toast.makeText(context, "slot1 if block", Toast.LENGTH_LONG).show();

			int im = controller.uncheckSlot(aDate, "slot1");

			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();
			} else {

				Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
			}

		} else if (oldTime.equals(s2)) {
			Toast.makeText(context, "slot2 if block", Toast.LENGTH_LONG).show();

			int im = controller.uncheckSlot(aDate, "slot2");

			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();
			} else {
				Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
			}

		} else if (oldTime.equals(s3)) {
			Toast.makeText(context, "slot3 if block", Toast.LENGTH_LONG).show();

			int im = controller.uncheckSlot(aDate, "slot3");
			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();
			} else {
				Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
			}

		} else if (oldTime.equals(s4)) {

			Toast.makeText(context, "slot4 if block", Toast.LENGTH_LONG).show();

			int im = controller.uncheckSlot(aDate, "slot4");

			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();
			} else {
				Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
			}

		} else if (oldTime.equals(s5)) {
			Toast.makeText(context, "slot5 if block", Toast.LENGTH_LONG).show();

			int im = controller.uncheckSlot(aDate, "slot5");

			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();
			}
		}

		else if (oldTime.equals(s6)) {

			Toast.makeText(context, "slot6 if block", Toast.LENGTH_LONG).show();

			int im = controller.uncheckSlot(aDate, "slot6");

			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();
			}
		} else if (oldTime.equals(s7)) {
			Toast.makeText(context, "slot7 if block", Toast.LENGTH_LONG).show();

			int im = controller.uncheckSlot(aDate, "slot7");

			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();
			}
		} else if (oldTime.equals(s8)) {
			Toast.makeText(context, "slot8 if block", Toast.LENGTH_LONG).show();

			int im = controller.uncheckSlot(aDate, "slot8");

			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();

			}
		}

		else if (oldTime.equals(s9)) {
			Toast.makeText(context, "slot9 if block", Toast.LENGTH_LONG).show();

			int im = controller.uncheckSlot(aDate, "slot9");

			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();
			}
		}

		else if (oldTime.equals(s10)) {
			Toast.makeText(context, "slot10 if block", Toast.LENGTH_LONG)
					.show();

			int im = controller.uncheckSlot(aDate, "slot10");

			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();
			}
		}

		else if (oldTime.equals(s11)) {
			Toast.makeText(context, "slot15 if block", Toast.LENGTH_LONG)
					.show();

			int im = controller.uncheckSlot(aDate, "slot11");

			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();
			}
		}

		else if (oldTime.equals(s12)) {
			Toast.makeText(context, "slot12 if block", Toast.LENGTH_LONG)
					.show();

			int im = controller.uncheckSlot(aDate, "slot12");

			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();
			}
		}
		
		else if (oldTime.equals(s13)) {
			
			
			Toast.makeText(context, "slot13 if block", Toast.LENGTH_LONG)
					.show();

			int im = controller.uncheckSlot(aDate, "slot13");

			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();
			}
		}

		else if (oldTime.equals(s14)) {
			Toast.makeText(context, "slot14 if block", Toast.LENGTH_LONG)
					.show();

			int im = controller.uncheckSlot(aDate, "slot14");

			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();
			}
		} else if (oldTime.equals(s15)) {
			Toast.makeText(context, "slot15 if block", Toast.LENGTH_LONG)
					.show();

			int im = controller.uncheckSlot(aDate, "slot15");

			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();
			}
		}

		else if (oldTime.equals(s16)) {
			Toast.makeText(context, "slot16 if block", Toast.LENGTH_LONG)
					.show();

			int im = controller.uncheckSlot(aDate, "slot16");

			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();
			}
		} else if (oldTime.equals(s17)) {
			Toast.makeText(context, "slot17 if block", Toast.LENGTH_LONG)
					.show();

			int im = controller.uncheckSlot(aDate, "slot17");

			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();
			}
		} else if (oldTime.equals(s18)) {
			Toast.makeText(context, "slot18 if block", Toast.LENGTH_LONG)
					.show();

			int im = controller.uncheckSlot(aDate, "slot18");

			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();
			}
		}

		else if (oldTime.equals(s19)) {
			Toast.makeText(context, "slot19 if block", Toast.LENGTH_LONG)
					.show();

			int im = controller.uncheckSlot(aDate, "slot19");

			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();
			}
		} else if (oldTime.equals(s20)) {
			Toast.makeText(context, "slot20 if block", Toast.LENGTH_LONG)
					.show();

			int im = controller.uncheckSlot(aDate, "slot20");

			if (im == 1) {

				Toast.makeText(context,
						"Slot unchecked at" + oldTime,
						Toast.LENGTH_LONG).show();
			}

		}

	}

}
